package com.Lgwd.controller.userController;


import com.Lgwd.entity.InitializeProfessionEntiy;
import com.Lgwd.entity.UserAccountEntiy;
import com.Lgwd.service.userService.InitializeProfessionService;
import com.Lgwd.service.userService.UserAccountService;
import com.Lgwd.utils.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * 用户注册接口
 */
@Slf4j
@RestController
@CrossOrigin
public class UserAccountController {


    /**
     * 角色账号
     */
    @Autowired
    UserAccountService userAccountService;


    /**
     * 角色初始化信息
     */
    @Autowired
    InitializeProfessionService initializeProfessionService;


    /**
     * 账号注册
     */
    @PostMapping("/register")
    public ResponseData register(@RequestBody UserAccountEntiy userAccount) {
        log.info("【注册接口录入信息】："+userAccount.toString());
        if (userAccount.getUser_account().equals("") || userAccount.getUser_account() == null) {
            return ResponseData.defaultFail("请输入账号");
        }
        if (userAccount.getUser_pwd().equals("") || userAccount.getUser_pwd() == null) {
            return ResponseData.defaultFail("请输入密码");
        }
        if (userAccount.getUser_profession().equals("") || userAccount.getUser_profession() == null) {
            return ResponseData.defaultFail("请选择职业 1：战士 2：法师");
        }

        Object user = userAccountService.findUserAccount(userAccount.getUser_account());
        if (user == null) {
            //注册账号
            userAccountService.addUserInfo(userAccount);
            //添加角色初始化属性信息
            initializeProfessionService.findProfessionInfo(userAccount);
        } else {
            return ResponseData.defaultFail("账号已存在");
        }

        return ResponseData.defaultSuccess("注册成功");
    }



}
